package com.hjy.cabinet.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hjy.cabinet.db.domain.DbAlarmLog;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class AlarmLogDbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "alarm_logs.db";
    private static final int DATABASE_VERSION = 30;

    private static final String TABLE_NAME = "alarm_log";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NUMBER = "number"; // 新增编号字段
    private static final String COLUMN_ALARM_LEVEL = "alarm_level";
    private static final String COLUMN_ALARM_REASON = "alarm_reason";
    private static final String COLUMN_STAT_TIME = "stat_time";
    private static final String COLUMN_END_TIME = "end_time";
    private static final String COLUMN_ALARM_TYPE = "alarm_type";
    private static final String COLUMN_CONFIRM_STATUS = "confirm_status";
    private static final String COLUMN_ERASE_STATUS = "erase_status";
    private static final String COLUMN_SYNC = "sync"; // 新增字段

    public AlarmLogDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_NUMBER + " TEXT UNIQUE, "   // 添加UNIQUE约束
                + COLUMN_ALARM_LEVEL + " TEXT, "
                + COLUMN_ALARM_REASON + " TEXT, "
                + COLUMN_STAT_TIME + " TEXT, "
                + COLUMN_END_TIME + " TEXT, "
                + COLUMN_ALARM_TYPE + " TEXT, "
                + COLUMN_CONFIRM_STATUS + " TEXT, "
                + COLUMN_ERASE_STATUS + " TEXT, "
                + COLUMN_SYNC + " INTEGER DEFAULT 0)"; // 使用 INTEGER 存储 Boolean
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    // 增加一条日志
    public long addAlarmLog(DbAlarmLog log) {



        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NUMBER, log.getNumber()); // 添加编号
        values.put(COLUMN_ALARM_LEVEL, log.getAlarmLevel());
        values.put(COLUMN_ALARM_REASON, log.getAlarmReason());
        values.put(COLUMN_STAT_TIME, log.getStatTime());
        values.put(COLUMN_END_TIME, log.getEndTime());
        values.put(COLUMN_ALARM_TYPE, log.getAlarmType());
        values.put(COLUMN_CONFIRM_STATUS, log.getConfirmStatus());
        values.put(COLUMN_ERASE_STATUS, log.getEraseStatus());
        values.put(COLUMN_SYNC, log.getSync() ? 1 : 0); // 将 Boolean 转为 INTEGER
        long id = db.insert(TABLE_NAME, null, values);
        db.close();
        return id;
    }

    // 查询所有日志
    public List<DbAlarmLog> getAllAlarmLogs() {
        DbAlarmLog log;
        List<DbAlarmLog> logList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                log = new DbAlarmLog();
                log.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID))); // 使用 getColumnIndexOrThrow
                log.setNumber(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NUMBER))); // 获取编号
                log.setAlarmLevel(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_LEVEL)));
                log.setAlarmReason(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_REASON)));
                log.setStatTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_STAT_TIME)));
                log.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_END_TIME)));
                log.setAlarmType(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_TYPE)));
                log.setConfirmStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONFIRM_STATUS)));
                log.setEraseStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ERASE_STATUS)));
                log.setSync(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_SYNC)) == 1); // 将 INTEGER 转为 Boolean
                logList.add(log);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return logList;
    }

    // 根据 ID 查询日志
    public DbAlarmLog getAlarmLogById(int id) {
        DbAlarmLog log = null;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, COLUMN_ID + " = ?", new String[]{String.valueOf(id)}, null, null, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                log = new DbAlarmLog();
                log.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID))); // 使用 getColumnIndexOrThrow
                log.setNumber(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NUMBER))); // 获取编号
                log.setAlarmLevel(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_LEVEL)));
                log.setAlarmReason(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_REASON)));
                log.setStatTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_STAT_TIME)));
                log.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_END_TIME)));
                log.setAlarmType(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_TYPE)));
                log.setConfirmStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONFIRM_STATUS)));
                log.setEraseStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ERASE_STATUS)));
                log.setSync(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_SYNC)) == 1); // 将 INTEGER 转为 Boolean
            }
            cursor.close();
        }
        db.close();
        return log;
    }

    // 根据 number 查询日志
    public DbAlarmLog getAlarmLogByNumber(String number) {
        DbAlarmLog log = null;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, COLUMN_NUMBER + " = ?", new String[]{String.valueOf(number)}, null, null, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                log = new DbAlarmLog();
                log.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID))); // 使用 getColumnIndexOrThrow
                log.setNumber(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NUMBER))); // 获取编号
                log.setAlarmLevel(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_LEVEL)));
                log.setAlarmReason(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_REASON)));
                log.setStatTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_STAT_TIME)));
                log.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_END_TIME)));
                log.setAlarmType(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_TYPE)));
                log.setConfirmStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONFIRM_STATUS)));
                log.setEraseStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ERASE_STATUS)));
                log.setSync(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_SYNC)) == 1); // 将 INTEGER 转为 Boolean
            }
            cursor.close();
        }
        db.close();
        return log;
    }

    // 更新一条日志
    public int updateAlarmLog(DbAlarmLog log) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NUMBER, log.getNumber()); // 更新编号
        values.put(COLUMN_ALARM_LEVEL, log.getAlarmLevel());
        values.put(COLUMN_ALARM_REASON, log.getAlarmReason());
        values.put(COLUMN_STAT_TIME, log.getStatTime());
        values.put(COLUMN_END_TIME, log.getEndTime());
        values.put(COLUMN_ALARM_TYPE, log.getAlarmType());
        values.put(COLUMN_CONFIRM_STATUS, log.getConfirmStatus());
        values.put(COLUMN_ERASE_STATUS, log.getEraseStatus());
        values.put(COLUMN_SYNC, log.getSync() ? 1 : 0); // 更新 sync 字段的值
        int rowsAffected = db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[]{String.valueOf(log.getId())});
        db.close();
        return rowsAffected;
    }

    // 删除一条日志
    public int deleteAlarmLog(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        int delete = db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
        db.close();
        return delete;
    }

    // 查询 sync 为 false 的所有日志
    public List<DbAlarmLog> getAllUnsyncedAlarmLogs() {
        DbAlarmLog log;
        List<DbAlarmLog> logList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_SYNC + " = 0"; // sync = false
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                log = new DbAlarmLog();
                log.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID))); // 使用 getColumnIndexOrThrow
                log.setNumber(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NUMBER))); // 获取编号
                log.setAlarmLevel(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_LEVEL)));
                log.setAlarmReason(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_REASON)));
                log.setStatTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_STAT_TIME)));
                log.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_END_TIME)));
                log.setAlarmType(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ALARM_TYPE)));
                log.setConfirmStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONFIRM_STATUS)));
                log.setEraseStatus(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ERASE_STATUS)));
                log.setSync(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_SYNC)) == 1); // 将 INTEGER 转为 Boolean
                logList.add(log);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return logList;
    }





}